﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using  com.guojin.mj.net.message.club;
using com.guojin.mj.net.message.login;
using DG.Tweening;

public class GameHallPage : EventManager
{
    public static Texture2D headTexture2D;    //保存头像
    private string url;                                         //头像地址
    public Text name;
    public Text id;
    public RawImage headRawImg;
    public Text announcementTex;
    public Text roomCardTex;

    public Button roomCardBtn;
    public Button exitGameBtn;
    public Button createRoomBtn;
    public Button joinRoomBtn;
    public Button shareBtn;                             //分享按钮
    public Button msgBtn;                              //公告按钮
    public Button warRecordBtn;                    //战绩按钮
    public Button ruleBtn;                               //规则按钮
    public Button setBtn;                                 //设置按钮

    public override void InformationSetting()
    {
        //设置 name, id, 广播, 房卡, 下载头像
        SetText(name, GameData.GetInstance().userName);
        SetText(id, string.Format("ID:{0}", GameData.GetInstance().userID));
        SetText(announcementTex, GameData.GetInstance().announcement);
        SetText(roomCardTex, RoomCard.GetInstance().RoomCardCounts.ToString());
        DownloadHeadTexture(headRawImg.gameObject, GameData.GetInstance().avatar);
    }

    void Start()
    {
        AddListenerToButton(roomCardBtn, RoomCardBtnClick);
        AddListenerToButton(exitGameBtn, ExitGameBtnClick);
        AddListenerToButton(createRoomBtn, CreatRoomBtnClick);
        AddListenerToButton(joinRoomBtn, JoinRoomBtnClick);
        AddListenerToButton(shareBtn, ShareBtnClick);
        AddListenerToButton(msgBtn, ServiceBtnClick);
        AddListenerToButton(warRecordBtn, WarRecordBtnClick);
        AddListenerToButton(ruleBtn, RuleBtnClick);
        AddListenerToButton(setBtn, SetBtnClick);
    }

    void RoomCardBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.BuyRoomCardPage, (GameObject obj) =>
            {
                obj.GetComponent<BuyRoomCardPage>().InformationSetting();
            });

        MessageManager.GetInstance().SendMsg729();
    }

    public void RefreshRoomCardText()
    {
        SetText(roomCardTex, RoomCard.GetInstance().RoomCardCounts.ToString());
    }

    void ExitGameBtnClick()
    {
        GameObject.Find("StaticSource").GetComponent<MainLogic>().ShowExitGameTipsPage();
    }

    void CreatRoomBtnClick()              
    {
        UIManager.ChangeUI(UIManager.PageState.CreateRoomPage, (GameObject obj) =>
         {
             obj.GetComponent<CreateRoomPage>().InformationSetting();
         });

        MessageManager.GetInstance().SendMsg756();
    }    

    void JoinRoomBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.JoinRoomPage, (GameObject obj) =>
        {
            obj.GetComponent<JoinRoomPage>().InformationSetting();
        });
    }

    void ShareBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.SharePage, (GameObject obj) =>
        {
            obj.GetComponent<SharePage>().InformationSetting();
        });
    }

    void ServiceBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.ServicePage, (GameObject obj) =>
        {
            obj.GetComponent<ServicePage>().InformationSetting();
        });

        MessageManager.GetInstance().SendMsg729();
    }

    void WarRecordBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.WarRecordPage, (GameObject obj) =>
        {
            obj.GetComponent<WarRecordPage>().InformationSetting();
        });

        MessageManager.GetInstance().SendMsg719(GameData.GetInstance().userID);
    }

    void RuleBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.RulePage, delegate (GameObject target)
        {
            target.GetComponent<GameHallShowRulePage>().InformationSetting();
        });
    }

    void SetBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.GameSetPage, delegate (GameObject target)
        {
            target.GetComponent<GameSetPage>().InformationSetting();
        });
    }
}
